package com.platform.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.CommentPictureVo;
import com.platform.entity.CommentVo;
import com.platform.entity.CouponVo;
import com.platform.entity.UserCouponVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiCommentPictureService;
import com.platform.service.ApiCommentService;
import com.platform.service.ApiCouponService;
import com.platform.service.ApiUserCouponService;
import com.platform.service.ApiUserService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Base64;
import com.platform.utils.CharUtil;
import com.platform.utils.Query;
import com.platform.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者: @author Harmon <br> 时间: 2017-08-11 08:32<br> 描述: ApiIndexController <br>
 */
@Api(tags = "评论")
@RestController
@RequestMapping("/api/comment")
public class ApiCommentController extends ApiBaseAction {

  @Autowired
  private ApiCommentService commentService;
  @Autowired
  private ApiUserService userService;
  @Autowired
  private ApiCommentPictureService commentPictureService;
  @Autowired
  private ApiCouponService apiCouponService;
  @Autowired
  private ApiUserCouponService apiUserCouponService;

  /**
   * 发表评论
   */
  @ApiOperation(value = "发表评论")
  @PostMapping("post")
  public Object post(@LoginUser UserVo loginUser) {
    Map resultObj = new HashMap();
    //
    JSONObject jsonParam = getJsonRequest();
    Integer typeId = jsonParam.getInteger("typeId");
    Integer valueId = jsonParam.getInteger("valueId");
    String content = jsonParam.getString("content");
    JSONArray imagesList = jsonParam.getJSONArray("imagesList");
    CommentVo commentEntity = new CommentVo();
    commentEntity.setType_id(typeId);
    commentEntity.setValue_id(valueId);
    commentEntity.setContent(content);
    commentEntity.setStatus(0);
    //
    commentEntity.setAdd_time(System.currentTimeMillis() / 1000);
    commentEntity.setUser_id(loginUser.getUserId());
    commentEntity.setContent(Base64.encode(commentEntity.getContent()));
    Integer insertId = commentService.save(commentEntity);
    //
    if (insertId > 0 && null != imagesList && imagesList.size() > 0) {
      int i = 0;
      for (Object imgLink : imagesList) {
        i++;
        CommentPictureVo pictureVo = new CommentPictureVo();
        pictureVo.setComment_id(insertId);
        pictureVo.setPic_url(imgLink.toString());
        pictureVo.setSort_order(i);
        commentPictureService.save(pictureVo);
      }
    }
    // 是否领取优惠券
    if (insertId > 0 && typeId == 0) {
      // 当前评价的次数
      Map param = new HashMap();
      param.put("value_id", valueId);
      List<CommentVo> commentVos = commentService.queryList(param);
      boolean hasComment = false;
      for (CommentVo commentVo : commentVos) {
        if (commentVo.getUser_id().equals(loginUser.getUserId())
            && !commentVo.getId().equals(insertId)) {
          hasComment = true;
        }
      }
      if (!hasComment) {
        Map couponParam = new HashMap();
        couponParam.put("send_type", 6);
        CouponVo newCouponConfig = apiCouponService.queryMaxUserEnableCoupon(couponParam);
        if (null != newCouponConfig
            && newCouponConfig.getMin_transmit_num() >= commentVos.size()) {
          UserCouponVo userCouponVo = new UserCouponVo();
          userCouponVo.setAdd_time(new Date());
          userCouponVo.setCoupon_id(newCouponConfig.getId());
          userCouponVo.setCoupon_number(CharUtil.getRandomString(12));
          userCouponVo.setUser_id(loginUser.getUserId());
          apiUserCouponService.save(userCouponVo);
          resultObj.put("coupon", newCouponConfig);
        }
      }
    }
    if (insertId > 0) {
      return toResponsObject(0, "评论添加成功", resultObj);
    } else {
      return toResponsFail("评论保存失败");
    }
  }

  /**
   *
   */
  @ApiOperation(value = "评论数量")
  @GetMapping("count")
  public Object count(Integer typeId, Integer valueId) {
    Map<String, Object> resultObj = new HashMap();
    //
    Map param = new HashMap();
    param.put("type_id", typeId);
    param.put("value_id", valueId);
    Integer allCount = commentService.queryTotal(param);
    Integer hasPicCount = commentService.queryhasPicTotal(param);
    //
    resultObj.put("allCount", allCount);
    resultObj.put("hasPicCount", hasPicCount);
    return toResponsSuccess(resultObj);
  }

  /**
   * @param showType 选择评论的类型 0 全部， 1 只显示图片
   */
  @ApiOperation(value = "选择评论类型")
  @IgnoreAuth
  @GetMapping("list")
  public Object list(Integer typeId, Integer valueId, Integer showType,
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "size", defaultValue = "10") Integer size,
      String sort, String order) {
    Map<String, Object> resultObj = new HashMap();
    List<CommentVo> commentList = new ArrayList();
    Map param = new HashMap();
    param.put("type_id", typeId);
    param.put("value_id", valueId);
    param.put("page", page);
    param.put("limit", size);
    if (StringUtils.isNullOrEmpty(sort)) {
      param.put("order", "desc");
    } else {
      param.put("order", sort);
    }
    if (StringUtils.isNullOrEmpty(order)) {
      param.put("sidx", "id");
    } else {
      param.put("sidx", order);
    }
    if (null != showType && showType == 1) {
      param.put("hasPic", 1);
    }
    //查询列表数据
    Query query = new Query(param);
    commentList = commentService.queryList(query);
    int total = commentService.queryTotal(query);
    ApiPageUtils pageUtil = new ApiPageUtils(commentList, total, query.getLimit(), query.getPage());
    //
    for (CommentVo commentItem : commentList) {
      commentItem.setContent(Base64.decode(commentItem.getContent()));
      commentItem.setUser_info(userService.queryObject(commentItem.getUser_id()));

      Map paramPicture = new HashMap();
      paramPicture.put("comment_id", commentItem.getId());
      List<CommentPictureVo> commentPictureEntities = commentPictureService.queryList(paramPicture);
      commentItem.setPic_list(commentPictureEntities);
    }
    return toResponsSuccess(pageUtil);
  }
}